package com.yunao.web.convert;



import com.yunao.entity.Subject;
import com.yunao.web.form.QueryForm;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class QueryForm2Map {

    public static Map<String,String> searchConvert(QueryForm queryForm, Subject subject){
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotEmpty(queryForm.getWord())){
            if (StringUtils.isEmpty(queryForm.getRemoveWord())){
                map.put("keywords",queryForm.getWord());
            }else {
                map.put("keywords",queryForm.getWord()+"-"+queryForm.getRemoveWord());
            }
        }else {
            if (subject!=null) {
                if (StringUtils.isEmpty(queryForm.getRemoveWord())) {
                    map.put("keywords", subject.getWord());
                    if (StringUtils.isNotEmpty(subject.getRemoveWord())) {
                        map.put("keywords", subject.getWord() + "-" + subject.getRemoveWord());
                    }
                } else {
                    map.put("keywords", subject.getWord() + "-" + queryForm.getRemoveWord());
                }
            }

        }
        String timeFreq = queryForm.getTimeFreq()==null?"3":queryForm.getTimeFreq();
        String begin = queryForm.getTimebegin();
        String end = queryForm.getTimeend();
        if (StringUtils.isEmpty(begin) && StringUtils.isEmpty(end)){
            map.put("days",timeFreq);
        }else if (!StringUtils.isEmpty(begin)){
            if (!StringUtils.isEmpty(end)) {
                Date beginDate = null;
                Date endDate = null;
                try {
                    beginDate = sdf.parse(begin);
                    endDate = sdf.parse(end);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                map.put("between", beginDate.getTime() + "," + endDate.getTime());
            }else {
                Date beginDate = null;
                try {
                    beginDate = sdf.parse(begin);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                map.put("greaterequal",beginDate.getTime());
            }
        }
        if (StringUtils.isNotEmpty(queryForm.getLocation())){
            map.put("location",queryForm.getLocation());
        }else {
            if(subject!=null){
                if (StringUtils.isNotEmpty(subject.getSubjectLocaltion())){
                    map.put("location",subject.getSubjectLocaltion());
                }
            }
        }
        if (!StringUtils.isEmpty(queryForm.getDomainLevel())){
            map.put("domain_level",queryForm.getDomainLevel());
        }
        if (!StringUtils.isEmpty(queryForm.getMessageType())){
            String MTs = queryForm.getMessageType();
            String MTZhengWen= "主题帖,一般新闻,微博正文,微信正文,博文" ;
            String MTHuiTie= "回帖,新闻评论 ";
            if(MTs.equals("仅评论回帖")){
                MTZhengWen = MTHuiTie;
            }
            map.put("message_type",MTZhengWen);
        }
        String searchLoc = StringUtils.isEmpty(queryForm.getSearchLoc())?"正文":queryForm.getSearchLoc();
        if (searchLoc.equals("标题")){
            map.put("title",queryForm.getWord());
        }
        if (!StringUtils.isEmpty(queryForm.getAuthor())){
            map.put("author",queryForm.getAuthor());
        }
        if (!StringUtils.isEmpty(queryForm.getDomain())){
            map.put("domain",queryForm.getDomain());
        }
        if (!StringUtils.isEmpty(queryForm.getSort())){
            map.put("sort",queryForm.getSort());
        }
        if (!StringUtils.isEmpty(queryForm.getEmotion())){
            map.put("emotion",queryForm.getEmotion());
        }
        if (!StringUtils.isEmpty(queryForm.getCc())){
            map.put("channel",queryForm.getCc());
        }
        String reprint = StringUtils.isEmpty(queryForm.getReprint())?"否":queryForm.getReprint();
        if (reprint.equals("是")){
            map.put("distance","0.5");
        }
        int pageSize = queryForm.getPageSize()==0?20:queryForm.getPageSize();
        if (queryForm.getPageSize()!=0){
            map.put("limit",pageSize+"");
        }
        if (queryForm.getPage()>1){
            map.put("offset",(queryForm.getPage()-1)*pageSize+"");
        }
        return map;
    }


    public static Map<String,String> analysisConvert(QueryForm queryForm){
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (queryForm.getWord()!=null){
            map.put("keywords",queryForm.getWord());
        }
        String timeFreq = queryForm.getTimeFreq()==null?"3":queryForm.getTimeFreq();
        String begin = queryForm.getTimebegin()==null? new Date().toString():queryForm.getTimebegin();
        String end = queryForm.getTimeend()==null?new Date().toString():queryForm.getTimeend();
        Date beginDate = null;
        Date endDate = null;
        if (begin==null && end==null){
            try {
                beginDate = sdf.parse(begin);
                endDate = sdf.parse(end);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            map.put("days",timeFreq);
        }else {
            map.put("between",new long[]{beginDate.getTime(),endDate.getTime()});
        }
        if (queryForm.getLocation()!=null){
            map.put("location",queryForm.getLocation());
        }
        if (queryForm.getDomainLevel()!=null){
            map.put("domain_level",queryForm.getDomainLevel());
        }
        if (queryForm.getMessageType()!=null){
            map.put("message_type",queryForm.getMessageType());
        }
        if (queryForm.getSearchLoc().equals("标题")){
            map.put("title",queryForm.getWord());
        }
        if (queryForm.getAuthor()!=null){
            map.put("author",queryForm.getAuthor());
        }
        if (queryForm.getDomain()!=null){
            map.put("domain",queryForm.getDomain());
        }
        if (queryForm.getSort()!=null){
            map.put("sort",queryForm.getSort());
        }
        if (queryForm.getEmotion()!=null){
            map.put("emotion",queryForm.getEmotion());
        }
        if (queryForm.getCc()!=null){
            map.put("channel",queryForm.getCc());
        }
        return map;
    }
}
